package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.constant.OneLevelAuditConstant;
import com.xueyi.aixihu.constant.SystemRoleConstant;
import com.xueyi.aixihu.constant.ThreeAssetConstant;
import com.xueyi.aixihu.constant.TwoLevelAuditConstant;
import com.xueyi.aixihu.entity.vo.AxhFinanceVo;
import com.xueyi.aixihu.entity.vo.DataList;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.entity.vo.FindWaitAuditReportFormListVo;
import com.xueyi.aixihu.constant.*;
import com.xueyi.aixihu.entity.po.AxhFinanceLog;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.FinanceMapper;
import com.xueyi.aixihu.mapper.ReportFormsAuditMapper;
import com.xueyi.aixihu.reqeust.*;
import com.xueyi.aixihu.mapper.ReportFormsMapper;
import com.xueyi.aixihu.service.ReportFormsAuditService;
import com.xueyi.aixihu.status.SystemCode;
import org.apache.ibatis.annotations.One;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author hxs
 * @createDate 2023/6/29 11:07
 * @description 财务三资审核接口 【参与角色】镇财政所成员、镇财政所所长
 */
@Service
public class ReportFormsAuditServiceImpl implements ReportFormsAuditService {

    @Resource(type = ReportFormsAuditMapper.class)
    private ReportFormsAuditMapper reportFormsAuditMapper;

    @Resource(type = ReportFormsMapper.class)
    private ReportFormsMapper reportFormsMapper;

    @Resource(type = FinanceMapper.class)
    private FinanceMapper financeMapper;

    /**
     * 查询待审核报表
     *
     * @param findWaitAuditReportFormListReq 参数
     * @return 列表
     */
    @Override
    public DataList<FindWaitAuditReportFormListVo> findWaitAuditReportFormList(FindWaitAuditReportFormListReq findWaitAuditReportFormListReq) throws SzException {
        findWaitAuditReportFormListReq.format();

        Integer reportType = findWaitAuditReportFormListReq.getReportType();

        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();

        //当前用户的管辖范围
        String areaIds = "";

        //镇级 由镇财政所所长审核
//        if (ThreeAssetConstant.CW.equals(reportType)) {
        if (findWaitAuditReportFormListReq.getAddressType().equals(1)) {
            if (!SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageTownId();
        }
        //村、社区 由镇财政所所长、镇财政所成员审核
//        else if (ThreeAssetConstant.ZJ.equals(reportType) || ThreeAssetConstant.ZC.equals(reportType) || ThreeAssetConstant.ZY.equals(reportType)) {
        else if (findWaitAuditReportFormListReq.getAddressType().equals(2) || findWaitAuditReportFormListReq.getAddressType().equals(3)) {
            //镇财政所所长、镇财政所成员
            if (SystemRoleConstant.Z_CZS_SZ.equals(role) || SystemRoleConstant.Z_CZS_CY.equals(role)) {
                areaIds = user.findUserManageCommunityId();
            } else {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        if (!StringUtils.hasLength(areaIds)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        Integer currentStatus = findWaitAuditReportFormListReq.getCurrentStatus();
        if (currentStatus.equals(0)) {
            if (SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.ONE_CONSENT + "");
            } else {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.SUBMIT_AUDIT + "," + TwoLevelAuditConstant.TWO_FAIL);
            }
        } else if (currentStatus.equals(1)) {
            if (SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.TWO_CONSENT + "");
            } else {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.ONE_CONSENT + "");
            }
        } else if (currentStatus.equals(2)) {
            if (SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.TWO_FAIL + "");
            } else {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.ONE_FAIL + "");
            }
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }


        Integer total = reportFormsAuditMapper.findWaitAuditReportFormListTotal(findWaitAuditReportFormListReq, areaIds);
        List<FindWaitAuditReportFormListVo> list = new ArrayList<>();
        if (total > 0) {
            list = reportFormsAuditMapper.findWaitAuditReportFormList(findWaitAuditReportFormListReq, areaIds);
        }

        if (!CollectionUtils.isEmpty(list)) {
            for (FindWaitAuditReportFormListVo item : list) {
                Integer current = item.getCurrentStatus();
//                if (SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                if (AddressConstant.CUN.equals(findWaitAuditReportFormListReq.getAddressType()) || AddressConstant.SQ.equals(findWaitAuditReportFormListReq.getAddressType()) || AddressConstant.JW.equals(findWaitAuditReportFormListReq.getAddressType())) {
                    if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(current) || TwoLevelAuditConstant.ONE_FAIL.equals(current) || TwoLevelAuditConstant.DRAFT.equals(current)) {
                        item.setCurrentStatusStr("待提交");
                    } else if (TwoLevelAuditConstant.ONE_CONSENT.equals(current)) {
                        item.setCurrentStatusStr("待审核");
                    } else if (TwoLevelAuditConstant.TWO_CONSENT.equals(current)) {
                        item.setCurrentStatusStr("已通过");
                    } else if (TwoLevelAuditConstant.TWO_FAIL.equals(current)) {
                        item.setCurrentStatusStr("已驳回");
                    } else {
                        item.setCurrentStatusStr("审核中");
                    }
                } else {
                    if (TwoLevelAuditConstant.DRAFT.equals(current)) {
                        item.setCurrentStatusStr("待提交");
                    } else if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(current)) {
                        item.setCurrentStatusStr("待审核");
                    } else if (TwoLevelAuditConstant.ONE_FAIL.equals(current)) {
                        item.setCurrentStatusStr("已驳回");
                    } else {
                        item.setCurrentStatusStr("已通过");
                    }
                }
            }
        }

        return new DataList<>(list, total);
    }

    /**
     * 查询镇级待审核财务报表
     *
     * @param findTownWaitAuditFinanceListReq 参数
     * @return 列表
     */
    @Override
    public DataList<FindWaitAuditReportFormListVo> findTownWaitAuditFinanceList(FindTownWaitAuditFinanceListReq findTownWaitAuditFinanceListReq) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        findTownWaitAuditFinanceListReq.format();

        //获取用户的角色 并判断是否是镇财政所所长
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        //当前用户的管辖范围
        String areaIds = "";

        //非超级管理员需要做判断
        if(!userMaxRole.equals(SystemRoleConstant.SUPER_ADMIN)){
            if (!userMaxRole.equals(SystemRoleConstant.Z_CZS_SZ) && !userMaxRole.equals(SystemRoleConstant.SUPER_ADMIN)) {
                throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
            }

            areaIds = dtUsersVo.findUserManageTownId();
            if (!StringUtils.hasLength(areaIds) || areaIds.equals("-111")) {
                throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
            }
        }

        Integer total = reportFormsAuditMapper.findTownWaitAuditFinanceListTotal(findTownWaitAuditFinanceListReq, areaIds, AddressConstant.ZJ);
        List<FindWaitAuditReportFormListVo> list = new ArrayList<>();
        if (total != null && total > 0) {
            list = reportFormsAuditMapper.findTownWaitAuditFinanceList(findTownWaitAuditFinanceListReq, areaIds, AddressConstant.ZJ);
            if (!CollectionUtils.isEmpty(list)) {
                for (FindWaitAuditReportFormListVo findWaitAuditReportFormListVo : list) {
                    if (findWaitAuditReportFormListVo.getCurrentStatus().equals(OneLevelAuditConstant.SUBMIT_AUDIT)) {
                        findWaitAuditReportFormListVo.setCurrentStatusStr("审核中");
                    } else if (findWaitAuditReportFormListVo.getCurrentStatus().equals(OneLevelAuditConstant.CONSENT)) {
                        findWaitAuditReportFormListVo.setCurrentStatusStr("已通过");
                    }
                }
            }
        }

        return new DataList<>(list, total);
    }

    /**
     * 通过审核
     *
     * @param agreePassAuditReq 参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void agreePassAudit(AgreePassAuditReq agreePassAuditReq) throws SzException {

        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();

        if (agreePassAuditReq.getType().equals(2)) {
            if (!StringUtils.hasLength(agreePassAuditReq.getRefuseMsg())) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "请输入拒绝理由~~~");
            }
        }


        AxhFinanceVo financeVo = reportFormsMapper.findReportFormById(agreePassAuditReq.getId());
        if (null == financeVo) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "不存在~~~");
        }

        String areaId = user.findUserManageCommunityId();

        //报表不属于当前用户的管辖范围
        if (!StringUtils.hasLength(areaId) || !("," + areaId + ",").contains("," + financeVo.getFromToAreaId() + ",")) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        Integer currentStatus = financeVo.getCurrentStatus();

        //成员
        if (SystemRoleConstant.Z_CZS_CY.equals(role)) {
            if (!(TwoLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus) || TwoLevelAuditConstant.TWO_FAIL.equals(currentStatus))) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            financeVo.setCurrentStatus(agreePassAuditReq.getType().equals(1) ? TwoLevelAuditConstant.ONE_CONSENT : TwoLevelAuditConstant.ONE_FAIL);
        }
        //所长
        else if (SystemRoleConstant.Z_CZS_SZ.equals(role)) {
            if (!TwoLevelAuditConstant.ONE_CONSENT.equals(currentStatus)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            financeVo.setCurrentStatus(agreePassAuditReq.getType().equals(1) ? TwoLevelAuditConstant.TWO_CONSENT : TwoLevelAuditConstant.TWO_FAIL);
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        financeVo.setFailMsg(agreePassAuditReq.getRefuseMsg());

        //修改报表
        reportFormsMapper.updateReportFormsAuditStatus(financeVo);
        //修改审核记录
        reportFormsMapper.updateReportFormsAuditRecordStatus(agreePassAuditReq.getAuditId(), financeVo.getCurrentStatus(), agreePassAuditReq.getRefuseMsg());

        //添加日志
        AxhFinanceLogVo axhFinanceLog = new AxhFinanceLogVo();
        axhFinanceLog.setFinanceId(financeVo.getId());
        axhFinanceLog.setCommandUserId(user.getId());
        String msg = "【" + (ThreeAssetConstant.CW.equals(agreePassAuditReq.getReportType()) ? "财务审核" : "三资审核") + "】" +
                "用户 " + user.getUsername() + (agreePassAuditReq.getType().equals(1) ? "同意" : "拒绝") + " 通过 《" + financeVo.getFinanceTitle() + "》的审核";

        if (agreePassAuditReq.getType().equals(2)) {
            msg += " , 拒绝理由：" + financeVo.getFailMsg();
        }
        axhFinanceLog.setLogContent(msg);
        axhFinanceLog.setCurrentFinanceStatus(financeVo.getCurrentStatus());
        axhFinanceLog.setRemark(agreePassAuditReq.getType().equals(1) ? "同意" : financeVo.getFailMsg());
        reportFormsMapper.addFinanceLog(axhFinanceLog);

    }

    /**
     * <<<<<<< HEAD
     * 查询村级的待审核报表
     *
     * @param findWaitAuditReportFormListReq
     * @return
     */
    @Override
    public DataList<FindWaitAuditReportFormListVo> findAreaFinanceList(FindWaitAuditReportFormListReq findWaitAuditReportFormListReq) throws SzException {
        findWaitAuditReportFormListReq.format();

        Integer reportType = findWaitAuditReportFormListReq.getReportType();

        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();

        //当前用户的管辖范围
        String areaIds = "";

        //镇级 由镇财政所所长审核
//        if (ThreeAssetConstant.CW.equals(reportType)) {
        if (findWaitAuditReportFormListReq.getAddressType().equals(1)) {
            if (!SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageTownId();
        }
        //村、社区 由镇财政所所长、镇财政所成员审核
//        else if (ThreeAssetConstant.ZJ.equals(reportType) || ThreeAssetConstant.ZC.equals(reportType) || ThreeAssetConstant.ZY.equals(reportType)) {
        else if (findWaitAuditReportFormListReq.getAddressType().equals(2) || findWaitAuditReportFormListReq.getAddressType().equals(3)) {
            //镇财政所所长、镇财政所成员
            if (SystemRoleConstant.Z_JGZ_ZZ.equals(role) || SystemRoleConstant.Z_JGZ_CY.equals(role)) {
                areaIds = user.findUserManageAreaId();
            } else {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        if (!StringUtils.hasLength(areaIds)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        Integer currentStatus = findWaitAuditReportFormListReq.getCurrentStatus();
        if (currentStatus.equals(0)) {
            if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.ONE_CONSENT + "");
            } else {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.SUBMIT_AUDIT + "," + TwoLevelAuditConstant.TWO_FAIL);
            }
        } else if (currentStatus.equals(1)) {
            if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.TWO_CONSENT + "");
            } else {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.ONE_CONSENT + "");
            }
        } else if (currentStatus.equals(2)) {
            if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.TWO_FAIL + "");
            } else {
                findWaitAuditReportFormListReq.setCurrentStatusStr(TwoLevelAuditConstant.ONE_FAIL + "");
            }
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }


        Integer total = reportFormsAuditMapper.findWaitAuditReportFormListTotal(findWaitAuditReportFormListReq, areaIds);
        List<FindWaitAuditReportFormListVo> list = new ArrayList<>();
        if (total > 0) {
            list = reportFormsAuditMapper.findWaitAuditReportFormList(findWaitAuditReportFormListReq, areaIds);
        }

        if (!CollectionUtils.isEmpty(list)) {
            for (FindWaitAuditReportFormListVo item : list) {
                Integer current = item.getCurrentStatus();
                if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
                    if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(current) || TwoLevelAuditConstant.ONE_FAIL.equals(current) || TwoLevelAuditConstant.DRAFT.equals(current)) {
                        item.setCurrentStatusStr("待提交");
                    } else if (TwoLevelAuditConstant.ONE_CONSENT.equals(current)) {
                        item.setCurrentStatusStr("待审核");
                    } else if (TwoLevelAuditConstant.TWO_CONSENT.equals(current)) {
                        item.setCurrentStatusStr("已通过");
                    } else if (TwoLevelAuditConstant.TWO_FAIL.equals(current)) {
                        item.setCurrentStatusStr("已驳回");
                    } else {
                        item.setCurrentStatusStr("审核中");
                    }
                } else {
                    if (TwoLevelAuditConstant.DRAFT.equals(current)) {
                        item.setCurrentStatusStr("待提交");
                    } else if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(current)) {
                        item.setCurrentStatusStr("待审核");
                    } else if (TwoLevelAuditConstant.ONE_FAIL.equals(current)) {
                        item.setCurrentStatusStr("已驳回");
                    } else {
                        item.setCurrentStatusStr("已通过");
                    }
                }
            }
        }

        return new DataList<>(list, total);


        //        findWaitAuditReportFormListReq.format();
//
//        DtUsersVo user = DtUsersVo.findCurrentUser();
//        Integer role = user.getUserMaxRole();
//
//        String id = user.findUserManageAreaId();
//        String communityId = user.findUserManageCommunityId();
//        String areaId = id + "," + communityId;
//
//        Integer reportType = findWaitAuditReportFormListReq.getReportType();
//        //三资审核 一级为镇经管站成员 一级审核状态为0
//        if (ThreeAssetConstant.ZJ.equals(reportType) || ThreeAssetConstant.ZY.equals(reportType) || ThreeAssetConstant.ZC.equals(reportType)) {
//            if (SystemRoleConstant.Z_JGZ_CY.equals(role)) {
//                findWaitAuditReportFormListReq.setCurrentStatus(OneLevelAuditConstant.SUBMIT_AUDIT);
//            } else if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
//                //二级审核 状态为同意
//                findWaitAuditReportFormListReq.setCurrentStatus(TwoLevelAuditConstant.ONE_CONSENT);
//            } else {
//                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "暂无权限");
//            }
//        } else {
//            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "暂无权限");
//        }
//        Integer total = reportFormsAuditMapper.findWaitAuditReportFormListTotal(findWaitAuditReportFormListReq, areaId);
//        List<FindWaitAuditReportFormListVo> list = new ArrayList<>();
//        if (total != null && total > 0) {
//            list = reportFormsAuditMapper.findWaitAuditReportFormList(findWaitAuditReportFormListReq, areaId);
//        }
//        return new DataList<>(list, total);
    }

    /**
     * 村级的三资审核（同意或拒绝）
     *
     * @param agreePassAuditReq 参数
     * @throws SzException
     */
    @Override
    public void agreeThreeAudit(AgreePassAuditReq agreePassAuditReq) throws SzException {
        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();

        if (agreePassAuditReq.getType().equals(2)) {
            if (!StringUtils.hasLength(agreePassAuditReq.getRefuseMsg())) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "请输入拒绝理由~~~");
            }
        }


        AxhFinanceVo financeVo = reportFormsMapper.findReportFormById(agreePassAuditReq.getId());
        if (null == financeVo) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "不存在~~~");
        }

        String areaId = user.findUserManageAreaId();

        //报表不属于当前用户的管辖范围
        if (!StringUtils.hasLength(areaId) || !("," + areaId + ",").contains("," + financeVo.getFromToAreaId() + ",")) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        Integer currentStatus = financeVo.getCurrentStatus();

        //镇经管站成员
        if (SystemRoleConstant.Z_JGZ_CY.equals(role)) {
            if (!TwoLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            financeVo.setCurrentStatus(agreePassAuditReq.getType().equals(1) ? TwoLevelAuditConstant.ONE_CONSENT : TwoLevelAuditConstant.ONE_FAIL);
        }
        //镇经管站所长
        else if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
            if (!TwoLevelAuditConstant.ONE_CONSENT.equals(currentStatus)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            financeVo.setCurrentStatus(agreePassAuditReq.getType().equals(1) ? TwoLevelAuditConstant.TWO_CONSENT : TwoLevelAuditConstant.TWO_FAIL);
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }


        //修改报表
        reportFormsMapper.updateReportFormsAuditStatus(financeVo);
        //修改审核记录
        reportFormsMapper.updateReportFormsAuditRecordStatus(agreePassAuditReq.getAuditId(), financeVo.getCurrentStatus(), agreePassAuditReq.getRefuseMsg());


        //添加日志
        AxhFinanceLogVo axhFinanceLog = new AxhFinanceLogVo();
        axhFinanceLog.setFinanceId(financeVo.getId());
        axhFinanceLog.setCommandUserId(user.getId());
        String msg = "【" + (ThreeAssetConstant.CW.equals(agreePassAuditReq.getReportType()) ? "财务审核" : "三资审核") + "】" +
                "用户 " + user.getUsername() + (agreePassAuditReq.getType().equals(1) ? "同意" : "拒绝") + " 通过 《" + financeVo.getFinanceTitle() + "》的审核";
        axhFinanceLog.setLogContent(msg);
        axhFinanceLog.setCurrentFinanceStatus(financeVo.getCurrentStatus());
        axhFinanceLog.setRemark(agreePassAuditReq.getType().equals(1) ? "同意" : financeVo.getFailMsg());
        reportFormsMapper.addFinanceLog(axhFinanceLog);
    }

    /**
     * 查询镇级财务报表页面数据
     *
     * @param findWaitFinanceSizeReq 类型
     * @return 页面数据
     */
    @Override
    public FindWaitFinanceSizeVo findWaitFinanceSize(FindWaitFinanceSizeReq findWaitFinanceSizeReq) throws SzException {


        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();

        Integer addressType = findWaitFinanceSizeReq.getAddressType();

        //镇、村、社区、居委会
        String address = "";
        if (addressType.equals(1)) {
            address = AddressConstant.ZJ + "";
        } else if (addressType.equals(2)) {
            address = AddressConstant.CUN + "";
        } else if (addressType.equals(3)) {
            address = AddressConstant.SQ + "," + AddressConstant.JW;
        }


        String areaIds = "";
        String waitAuditSizeStr = "";
        String agreeAuditSizeStr = "";
        String refuseAuditSize = "";


        //镇经管站站长
        if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
            areaIds = user.findUserManageAreaId();
            waitAuditSizeStr = TwoLevelAuditConstant.ONE_CONSENT + "";
            agreeAuditSizeStr = TwoLevelAuditConstant.TWO_CONSENT + "";
            refuseAuditSize = TwoLevelAuditConstant.TWO_FAIL + "";
        }
        //镇经管站成员
        else if (SystemRoleConstant.Z_JGZ_CY.equals(role)) {
            areaIds = user.findUserManageAreaId();
            waitAuditSizeStr = TwoLevelAuditConstant.SUBMIT_AUDIT + "," + TwoLevelAuditConstant.TWO_FAIL;
            agreeAuditSizeStr = TwoLevelAuditConstant.ONE_CONSENT + "";
            refuseAuditSize = TwoLevelAuditConstant.ONE_FAIL + "";
        }
        //镇财政所成员
        else if (SystemRoleConstant.Z_CZS_CY.equals(role)) {
            areaIds = user.findUserManageCommunityId();
            waitAuditSizeStr = TwoLevelAuditConstant.SUBMIT_AUDIT + "," + TwoLevelAuditConstant.TWO_FAIL;
            agreeAuditSizeStr = TwoLevelAuditConstant.ONE_CONSENT + "";
            refuseAuditSize = TwoLevelAuditConstant.ONE_FAIL + "";
        }
        //镇财政所所长
        else if (SystemRoleConstant.Z_CZS_SZ.equals(role)) {
            areaIds = user.findUserManageCommunityId();
            waitAuditSizeStr = TwoLevelAuditConstant.ONE_CONSENT + "";
            agreeAuditSizeStr = TwoLevelAuditConstant.TWO_CONSENT + "";
            refuseAuditSize = TwoLevelAuditConstant.TWO_FAIL + "";
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }


        if (!StringUtils.hasLength(areaIds)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        return reportFormsAuditMapper.findWaitFinanceSize(areaIds, address, findWaitFinanceSizeReq.getReportType(), waitAuditSizeStr, agreeAuditSizeStr, refuseAuditSize);

    }

    /**
     * 通过报表id查询报表
     *
     * @param id 报表id
     * @return 报表
     */
    @Override
    public AxhFinanceVo findFinanceById(Integer id) throws SzException {
        if (null == id || id < 1) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }
        AxhFinanceVo flag = reportFormsAuditMapper.findFinanceById(id);
        if (null == flag) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "未查询到数据");
        }
        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();

        Integer currentStatus = flag.getCurrentStatus();
//        if (SystemRoleConstant.CUN.equals(role) || SystemRoleConstant.SQ.equals(role)) {
//            if (TwoLevelAuditConstant.DRAFT.equals(currentStatus)) {
//                flag.setFinanceStatusStr("草稿");
//            } else if (TwoLevelAuditConstant.ONE_FAIL.equals(currentStatus)) {
//                flag.setFinanceStatusStr("已驳回");
//            } else if (TwoLevelAuditConstant.TWO_CONSENT.equals(currentStatus)) {
//                flag.setFinanceStatusStr("已通过");
//            } else {
//                flag.setFinanceStatusStr("审核中");
//            }
//        } else {
//            if (OneLevelAuditConstant.DRAFT.equals(currentStatus)) {
//                flag.setFinanceStatusStr("草稿");
//            } else if (OneLevelAuditConstant.FAIL.equals(currentStatus)) {
//                flag.setFinanceStatusStr("已驳回");
//            } else if (OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
//                flag.setFinanceStatusStr("已通过");
//            } else {
//                flag.setFinanceStatusStr("审核中");
//            }
//        }

        Integer areaType = flag.getAreaType();

       if (AddressConstant.ZJ.equals(areaType)) {
            if (OneLevelAuditConstant.DRAFT.equals(currentStatus)) {
                flag.setFinanceStatusStr("草稿");
            } else if (OneLevelAuditConstant.FAIL.equals(currentStatus)) {
                flag.setFinanceStatusStr("已驳回");
            } else if (OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
                flag.setFinanceStatusStr("已通过");
            } else {
                flag.setFinanceStatusStr("审核中");
            }
        } else {
            if (TwoLevelAuditConstant.DRAFT.equals(currentStatus)) {
                flag.setFinanceStatusStr("草稿");
            } else if (TwoLevelAuditConstant.ONE_FAIL.equals(currentStatus)) {
                flag.setFinanceStatusStr("已驳回");
            } else if (TwoLevelAuditConstant.TWO_CONSENT.equals(currentStatus)) {
                flag.setFinanceStatusStr("已通过");
            } else {
                flag.setFinanceStatusStr("审核中");
            }
        }


        FinanceBtnVo financeBtnVo = new FinanceBtnVo();
        financeBtnVo.setIsAgreePublicBtn(checkAgreePublicBtn(role, flag));
        financeBtnVo.setIsAgreeRefuseAuditBtn(checkAgreeRefuseAuditBtn(role, flag));
        financeBtnVo.setIsEditVoucher(checkEditVoucher(role, flag));
//        financeBtnVo.setIsAgainSubmit(checkAgainSubmit(role, flag));
        financeBtnVo.setIsTransferDraft(checkTransferDraft(flag, user));
        flag.setFinanceBtnVo(financeBtnVo);

        flag.setAxhAuditFinanceLogVos(this.findReportFormsAuditLog(id, Boolean.FALSE));

        return flag;
    }

    /**
     * 判断是否显示转为草稿
     *
     * @param flag 报表
     * @param user 用户
     */
    private Boolean checkTransferDraft(AxhFinanceVo flag, DtUsersVo user) {
        if (TwoLevelAuditConstant.ONE_FAIL.equals(flag.getCurrentStatus()) && user.getId().equals(flag.getCreateUserId())) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 是否现在再次提交
     *
     * @param role 结束
     * @param flag 报表
     */
    private Boolean checkAgainSubmit(Integer role, AxhFinanceVo flag) {
        Integer currentStatus = flag.getCurrentStatus();
        Integer areaType = flag.getAreaType();

        if (AddressConstant.ZJ.equals(areaType)) {
            if (SystemRoleConstant.Z_CZS_CY.equals(role) && OneLevelAuditConstant.FAIL.equals(currentStatus)) {
                return Boolean.TRUE;
            }
        } else {
            if (SystemRoleConstant.CUN.equals(role) && TwoLevelAuditConstant.ONE_FAIL.equals(currentStatus)) {
                return Boolean.TRUE;
            } else if (SystemRoleConstant.SQ.equals(role) && TwoLevelAuditConstant.ONE_FAIL.equals(currentStatus)) {
                return Boolean.TRUE;
            }
//            else if (SystemRoleConstant.Z_JGZ_CY.equals(role) && TwoLevelAuditConstant.TWO_FAIL.equals(currentStatus)) {
//                return Boolean.TRUE;
//            }  else if (SystemRoleConstant.Z_CZS_CY.equals(role) && TwoLevelAuditConstant.TWO_FAIL.equals(currentStatus)) {
//                return Boolean.TRUE;
//            }
        }

        return Boolean.FALSE;
    }

    /**
     * 判断可编辑凭证
     *
     * @param role 角色
     * @param flag 报表
     */
    private Boolean checkEditVoucher(Integer role, AxhFinanceVo flag) {
        Integer currentStatus = flag.getCurrentStatus();
        Integer areaType = flag.getAreaType();

        //一级审核
        if (AddressConstant.ZJ.equals(areaType)) {
            if (SystemRoleConstant.Z_CZS_CY.equals(role)) {
                if (OneLevelAuditConstant.DRAFT.equals(currentStatus)) {
                    return Boolean.TRUE;
                }
            }

        } else {
            if (AddressConstant.CUN.equals(areaType)) {
                if (SystemRoleConstant.CUN.equals(role)) {
                    if (TwoLevelAuditConstant.DRAFT.equals(currentStatus)) {
                        return Boolean.TRUE;
                    }
                }
            } else {
                if (SystemRoleConstant.SQ.equals(role)) {
                    if (TwoLevelAuditConstant.DRAFT.equals(currentStatus)) {
                        return Boolean.TRUE;
                    }
                }
            }

        }
        return Boolean.FALSE;
    }

    /**
     * 判断是否显示同意拒绝按钮
     *
     * @param role 角色
     * @param flag 报表
     * @return 是否显示
     */
    private Boolean checkAgreeRefuseAuditBtn(Integer role, AxhFinanceVo flag) {
        Integer areaType = flag.getAreaType();
        Integer currentStatus = flag.getCurrentStatus();
        //一级审核
        if (AddressConstant.ZJ.equals(areaType)) {
            if (!SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                return Boolean.FALSE;
            }
            if (OneLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        }
        //二级审核
        else if (AddressConstant.CUN.equals(areaType) || AddressConstant.SQ.equals(areaType) || AddressConstant.JW.equals(areaType)) {
            if (AddressConstant.CUN.equals(areaType)) {
                //镇经管站成员
                if (SystemRoleConstant.Z_JGZ_CY.equals(role)) {
                    if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
                        return Boolean.TRUE;
                    } else if (TwoLevelAuditConstant.TWO_FAIL.equals(currentStatus)) {
                        return Boolean.TRUE;
                    } else {
                        return Boolean.FALSE;
                    }
                }
                //镇经管站站长
                else if (SystemRoleConstant.Z_JGZ_ZZ.equals(role)) {
                    if (TwoLevelAuditConstant.ONE_CONSENT.equals(currentStatus)) {
                        return Boolean.TRUE;
                    } else {
                        return Boolean.FALSE;
                    }
                } else {
                    return Boolean.FALSE;
                }
            } else {
                if (SystemRoleConstant.Z_CZS_CY.equals(role)) {
                    if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
                        return Boolean.TRUE;
                    } else if (TwoLevelAuditConstant.TWO_FAIL.equals(currentStatus)) {
                        return Boolean.TRUE;
                    } else {
                        return Boolean.FALSE;
                    }
                } else if (SystemRoleConstant.Z_CZS_SZ.equals(role)) {
                    if (TwoLevelAuditConstant.ONE_CONSENT.equals(currentStatus)) {
                        return Boolean.TRUE;
                    } else {
                        return Boolean.FALSE;
                    }
                } else {
                    return Boolean.FALSE;
                }
            }


        } else {
            return Boolean.FALSE;
        }
    }

    /**
     * 判断是否显示公开按钮
     *
     * @param role 当前角色
     * @param flag 报表
     * @return 是否显示
     */
    private Boolean checkAgreePublicBtn(Integer role, AxhFinanceVo flag) {
        Integer areaType = flag.getAreaType();
        Integer currentStatus = flag.getCurrentStatus();
        if (AddressConstant.ZJ.equals(areaType)) {
            if (SystemRoleConstant.Z_CZS_SZ.equals(role) && OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
                return Boolean.TRUE;
            }
        } else {
            if (AddressConstant.CUN.equals(areaType) && SystemRoleConstant.CUN.equals(role) && TwoLevelAuditConstant.TWO_CONSENT.equals(currentStatus)) {
                return Boolean.TRUE;
            } else if (AddressConstant.SQ.equals(areaType) && SystemRoleConstant.SQ.equals(role) && TwoLevelAuditConstant.TWO_CONSENT.equals(currentStatus)) {
                return Boolean.TRUE;
            }
        }
//        if (SystemRoleConstant.CUN.equals(role) || SystemRoleConstant.SQ.equals(role) || SystemRoleConstant.Z_CZS_SZ.equals(role)) {
//            if ("已通过".equals(flag.getFinanceStatusStr())) {
//                return Boolean.TRUE;
//            } else {
//                return Boolean.FALSE;
//            }
//        } else {
//            return Boolean.FALSE;
//        }
        return Boolean.FALSE;

    }

    /**
     * 查询报表审核记录日志
     *
     * @param id   报表id
     * @param bool 是否需要反转
     * @return 日志列表
     */
    @Override
    public List<AxhAuditFinanceLogVo> findReportFormsAuditLog(Integer id, Boolean bool) throws SzException {
        if (null == id || id < 1) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }
        AxhFinanceVo flag = reportFormsAuditMapper.findFinanceById(id);
        if (null == flag) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "未查询到数据");
        }
        List<AxhAuditFinanceLogVo> list = new ArrayList<>();

        Integer currentStatus = flag.getCurrentStatus();

        Integer type = flag.getAreaType();

        //镇级 只有一级审核
        if (AddressConstant.ZJ.equals(type)) {
            //草稿
            if (OneLevelAuditConstant.DRAFT.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = new AxhAuditFinanceLogVo();
                item1.setTitle("待提交");
                item1.setWait(1);
                item1.setWaitStr(item1.getTitle());

                AxhAuditFinanceLogVo item2 = new AxhAuditFinanceLogVo();
                item2.setTitle("镇财政所所长");
                item2.setWait(1);


                list.add(item1);
                list.add(item2);
            }
            //已提交待审核
            else if (OneLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, OneLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = new AxhAuditFinanceLogVo();
                item2.setTitle("镇财政所所长");
                item2.setWait(1);

                list.add(item1);
                list.add(item2);
            }
            //同意
            else if (OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, OneLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = financeMapper.findFinanceLog(id, OneLevelAuditConstant.CONSENT);
                item2.setWait(2);
                item2.setTitle("镇财政所所长");


                list.add(item1);
                list.add(item2);
            }
            //拒绝
            else if (OneLevelAuditConstant.FAIL.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, OneLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = financeMapper.findFinanceLog(id, OneLevelAuditConstant.FAIL);
                item2.setWait(3);
                item2.setTitle("镇财政所所长");

                list.add(item1);
                list.add(item2);
            }
        }
        //社区、居委会 、村级
        else {
            if (TwoLevelAuditConstant.DRAFT.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = new AxhAuditFinanceLogVo();
                item1.setTitle("待提交");
                item1.setWait(1);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = new AxhAuditFinanceLogVo();
                item2.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站成员" : "镇财政所成员");
                item2.setWait(1);

                AxhAuditFinanceLogVo item3 = new AxhAuditFinanceLogVo();
                item3.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站站长" : "镇财政所所长");
                item3.setWait(1);

                list.add(item1);
                list.add(item2);
                list.add(item3);
            } else if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = new AxhAuditFinanceLogVo();
                item2.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站成员" : "镇财政所成员");
                item2.setWait(1);

                AxhAuditFinanceLogVo item3 = new AxhAuditFinanceLogVo();
                item3.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站站长" : "镇财政所所长");
                item3.setWait(1);

                list.add(item1);
                list.add(item2);
                list.add(item3);
            } else if (TwoLevelAuditConstant.ONE_CONSENT.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.ONE_CONSENT);
                item2.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站成员" : "镇财政所成员");
                item2.setWait(2);

                AxhAuditFinanceLogVo item3 = new AxhAuditFinanceLogVo();
                item3.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站站长" : "镇财政所所长");
                item3.setWait(1);

                list.add(item1);
                list.add(item2);
                list.add(item3);
            } else if (TwoLevelAuditConstant.ONE_FAIL.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.ONE_FAIL);
                item2.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站成员" : "镇财政所成员");
                item2.setWait(3);

                AxhAuditFinanceLogVo item3 = new AxhAuditFinanceLogVo();
                item3.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站站长" : "镇财政所所长");
                item3.setWait(1);

                list.add(item1);
                list.add(item2);
                list.add(item3);
            } else if (TwoLevelAuditConstant.TWO_CONSENT.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.ONE_CONSENT);
                item2.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站成员" : "镇财政所成员");
                item2.setWait(2);

                AxhAuditFinanceLogVo item3 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.TWO_CONSENT);
                item3.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站站长" : "镇财政所所长");
                item3.setWait(2);

                list.add(item1);
                list.add(item2);
                list.add(item3);
            } else if (TwoLevelAuditConstant.TWO_FAIL.equals(currentStatus)) {
                AxhAuditFinanceLogVo item1 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.SUBMIT_AUDIT);
                item1.setTitle("已提交");
                item1.setWait(0);
                item1.setWaitStr(item1.getTitle());


                AxhAuditFinanceLogVo item2 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.ONE_CONSENT);
                item2.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站成员" : "镇财政所成员");
                item2.setWait(2);

                AxhAuditFinanceLogVo item3 = financeMapper.findFinanceLog(id, TwoLevelAuditConstant.TWO_FAIL);
                item3.setTitle(AddressConstant.CUN.equals(type) ? "镇经管站站长" : "镇财政所所长");
                item3.setWait(3);

                list.add(item1);
                list.add(item2);
                list.add(item3);
            }

        }

        AxhAuditFinanceLogVo item3 = financeMapper.findFinanceLog(id, OneLevelAuditConstant.PUBLIC);
        if (null != item3) {
            item3.setTitle(item3.getRemark());
            item3.setWait("发布".equals(item3.getRemark()) ? 2 : 3);
            item3.setWaitStr("发布".equals(item3.getRemark()) ? "已发布" : "已下架");
        } else {
            item3 = new AxhAuditFinanceLogVo();
            item3.setWait(1);
            item3.setTitle("待发布");
            item3.setWaitStr("待发布");
        }
        list.add(item3);

        if (bool) {
            Collections.reverse(list);
        }
        return list;
    }

    /**
     * 修改报表信息
     *
     * @param updateReportFormInfoReq 参数
     */
    @Override
    public void updateReportFormInfo(UpdateReportFormInfoReq updateReportFormInfoReq) throws SzException {
        updateReportFormInfoReq.format();

        AxhFinanceVo flag = reportFormsAuditMapper.findFinanceById(updateReportFormInfoReq.getId());

        if (null == flag) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "未查询到数据~~~");
        }

        if (!TwoLevelAuditConstant.DRAFT.equals(flag.getCurrentStatus())) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "审核中~~~");
        }

        DtUsersVo user = DtUsersVo.findCurrentUser();
        if (!user.getId().equals(flag.getCreateUserId())) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        flag.setFinanceTitle(updateReportFormInfoReq.getFinanceTitle());
        flag.setCreateTimeStr(updateReportFormInfoReq.getCreateTime());
        flag.setSystemTypeId(updateReportFormInfoReq.getSystemTypeId());
        reportFormsAuditMapper.updateReportFormInfo(flag);

    }

    /**
     * 转化为草稿
     *
     * @param id 报表id
     */
    @Override
    public void transferDraft(Integer id) throws SzException {
        if (null == id || id < 1) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }

        AxhFinanceVo flag = reportFormsAuditMapper.findFinanceById(id);
        if (null == flag) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "未查询到数据~~~");
        }

        if (!TwoLevelAuditConstant.ONE_FAIL.equals(flag.getCurrentStatus())) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "审核中~~~");
        }

        DtUsersVo user = DtUsersVo.findCurrentUser();
        if (!user.getId().equals(flag.getCreateUserId())) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        reportFormsAuditMapper.transferDraft(id, TwoLevelAuditConstant.DRAFT);
    }

    /**
     * 查询不同状态的条数
     *
     * @param findTownWaitAuditFinanceListReq
     * @return
     */
    @Override
    public FinanceExpenseSizeVo findFinanceAuditTypeSize(FindTownWaitAuditFinanceListReq
                                                                 findTownWaitAuditFinanceListReq) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        findTownWaitAuditFinanceListReq.format();

        //获取用户的角色 并判断是否是镇财政所所长
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        if (!userMaxRole.equals(SystemRoleConstant.Z_CZS_SZ) && !userMaxRole.equals(SystemRoleConstant.SUPER_ADMIN)) {
            throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
        }

        //当前用户的管辖范围
        String areaIds = "";
        areaIds = dtUsersVo.findUserManageTownId();

        if (!StringUtils.hasLength(areaIds)) {
            throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
        }

        return reportFormsAuditMapper.findFinanceAuditTypeSize(findTownWaitAuditFinanceListReq, areaIds, AddressConstant.ZJ);
    }

}
